import {getRandomKey} from "./utils"
import forEach from "lodash/forEach"

export const listPage = {
	total: 0, // 总记录数
	size: 20, // 每页显示的记录数
	current: 1, // 当前页
	pages: 0, // 总页数
	list: []
};

export const getListPage = (data) => {
	const page = {};
	let tempObj = data;

	page.size = tempObj.maxResults;
	page.total = tempObj.resultCount;
	page.current = tempObj.current;
	page.pages = 0;
	if(tempObj.results){
		page.pages = (tempObj.resultCount % tempObj.maxResults) == 0 ?
			(tempObj.resultCount / tempObj.maxResults) : (
				parseInt(tempObj.resultCount / tempObj.maxResults) + 1);
	}
	page.list = tempObj.results?tempObj.results:[];
	forEach(page.list,item=>{
		if(item.id){
			item.key = `${item.id}`;
		}else{
			item.key = getRandomKey();
		}
	});
	return page;
};
export const setList = (oldListPage, newListPage) => {

	if (oldListPage) {
		let newObj = newListPage;

		if (newObj.current > 1 && oldListPage.list && oldListPage.list.length > 0) {
			let oldList = [...oldListPage.list];
			oldList = oldList.concat(newObj.list);
			newObj = {//合并newObj里面的list //assign({})
				...newObj,
				list: oldList
			};
		}
		// console.log('newObj:',newObj);
		return  newObj;
	}
};
